package com.aida.evil.eda.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aida.evil.aida.R;
import com.aida.evil.eda.Answer.ImageZoom;
import com.aida.evil.eda.Data.Fragment_Data;
import com.aida.evil.eda.MainActivity;

import yalantis.com.sidemenu.interfaces.ScreenShotable;

/**
 * Created by Konstantin on 22.12.2014.
 */
public class ContentFragment extends Fragment implements ScreenShotable
{
//    public static final String CLOSE = "Close";
//    public static final String BUILDING = "Building";
//    public static final String BOOK = "Book";
//    public static final String PAINT = "Paint";
//    public static final String CASE = "Case";
//    public static final String SHOP = "Shop";
//    public static final String PARTY = "Party";
//    public static final String MOVIE = "Movie";



    private static Fragment fragment=Fragment_Data.eda_Fragment;

    private View containerView;
    protected int res;
    protected String itemname;
    private Bitmap bitmap;

    public static ContentFragment newInstance(int resId)
    {
        ContentFragment contentFragment = new ContentFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(Integer.class.getName(), resId);
        contentFragment.setArguments(bundle);
        return contentFragment;
    }

    public static ContentFragment newFragment(String itemname)
    {
        ContentFragment contentFragment = new ContentFragment();
        Bundle bundle = new Bundle();

        bundle.putString("itemname", itemname);

        contentFragment.setArguments(bundle);
        return contentFragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        this.containerView = view.findViewById(R.id.container);
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        res = getArguments().getInt(Integer.class.getName());
        itemname = getArguments().getString("itemname");
        MainActivity mainActivity=new MainActivity();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        /*mImageView = (ImageView) rootView.findViewById(R.id.image_content);
        mImageView.setClickable(true);
        mImageView.setFocusable(true);
        mImageView.setImageResource(res);*/

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();


        if (itemname == "首页")
        {
            switchContent(fragment, Fragment_Data.eda_Fragment, fragmentManager, fragmentTransaction);
        }
        if (itemname == "成绩")
        {
            switchContent(fragment, Fragment_Data.score_Fragment, fragmentManager, fragmentTransaction);
        }
        if (itemname == "我的")
        {
            switchContent(fragment, Fragment_Data.my_Fragment, fragmentManager, fragmentTransaction);
        }
        if (itemname == "分享")
        {
            switchContent(fragment, Fragment_Data.share_Fragment, fragmentManager, fragmentTransaction);
        }
        if (itemname == "关于")
        {
            switchContent(fragment, Fragment_Data.about_Fragment, fragmentManager, fragmentTransaction);
        }

        fragmentTransaction.commit();
        return rootView;
    }

    /**
     * 判断fragment是否添加过,如果添加过,就隐藏然后显示,没添加过就添加
     *
     * @param from            旧的fragment
     * @param to              新的fragment
     * @param fragmentManager
     */
    private void switchContent(Fragment from, Fragment to, FragmentManager fragmentManager, FragmentTransaction fragmentTransaction)
    {
        if (fragment != to)
        {
            fragment = to;
            if (!to.isAdded())
            {    // 先判断是否被add过
                fragmentTransaction.hide(from).add(R.id.mainactivity_fragmentlinearlayout, to); // 隐藏当前的fragment，add下一个到Activity中
            }
            else
            {
                fragmentTransaction.hide(from).show(to); // 隐藏当前的fragment，显示下一个
            }
        }
    }


    @Override
    public void takeScreenShot()
    {
        Thread thread = new Thread()
        {
            @Override
            public void run()
            {
                Bitmap bitmap = Bitmap.createBitmap(containerView.getWidth(),
                        containerView.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                //containerView.draw(canvas);
                ContentFragment.this.bitmap = bitmap;
            }
        };

        thread.start();
    }

    @Override
    public Bitmap getBitmap()
    {
        return bitmap;
    }
}

